package cn.kgc.controller;

import cn.kgc.pojo.Role;
import cn.kgc.pojo.User;
import cn.kgc.service.userService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author: 张鹏飞
 * @date: 2022/2/10
 * @description:
 */
@Controller
@RequestMapping("/zpf")
public class UserController {
    @Resource
    private userService userService;

    @RequestMapping("/zpf_updRole_Menu")
    public String zpf_updRole_Menu() {
        return "zpf_updRole_Menu";
    }

    @RequestMapping("/index2")
    public ModelAndView index2(ModelAndView modelAndView, @RequestParam String userCode, @RequestParam String userPassword, HttpSession session) {



        boolean bo = userService.login(userCode, userPassword);
        if (bo == false) {
            modelAndView.addObject("error", "账号密码输入错误");

            modelAndView.setViewName("login01");
        } else if (bo == true) {
            Map<String, Object> map = userService.getMenu(userCode, userPassword);

            String userName=userService.zpf_getUserName(userCode,userPassword);
            String userRole=userService.zpf_getUserRole(userCode,userPassword);
            modelAndView.addObject("userRole",userRole);
            modelAndView.addObject("userName",userName);
            session.setAttribute("mingzi",userName);
            session.setAttribute("userRole",userRole);
            modelAndView.addObject("map", map);//保存的是数据
            modelAndView.setViewName("index");
        }
        return modelAndView;
    }


    @RequestMapping("/updPwd")
    @ResponseBody
    public String updPwd(@RequestParam String userCode, @RequestParam String oldPassword, @RequestParam String newPassword) {
        boolean bo = userService.updPassword(userCode, oldPassword, newPassword);
        return JSON.toJSONString(bo);
    }

    @RequestMapping("/updRole")
    @ResponseBody
    public String updPower(@RequestParam Integer rId, @RequestParam Integer id) {
        boolean bo = userService.updRole(rId, id);
        return JSON.toJSONString(bo);
    }

    @RequestMapping("/loginOut")
    public String loginOut(Model model) {
        return "redirect:/";
    }

    @RequestMapping("/selUserPage")
    @ResponseBody
    public String updRole(@RequestParam String userName, @RequestParam Integer currentPage, @RequestParam Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        Page<User> page = userService.selUsers(userName);
        PageInfo<User> pageInfo = new PageInfo<>(page);
        return JSON.toJSONString(pageInfo);
    }

    @RequestMapping("/selRole")
    @ResponseBody
    public String selRole() {
        List<Role> roleList = userService.selRoles();
        return JSON.toJSONString(roleList);
    }


    @RequestMapping("/selMenu")
    @ResponseBody
    public String selMenu() {
        return JSON.toJSONString(userService.selMenu());
    }

    @RequestMapping("/selMenuByrId")
    @ResponseBody
    public String selMenuByrId(@RequestParam Integer rId) {
        return JSON.toJSONString(userService.selMenuByRid(rId));
    }

    @RequestMapping("/selAllMenuByrId")
    @ResponseBody
    public String selAllMenuByrId(@RequestParam Integer rId) {
        return JSON.toJSONString(userService.selAllMenuByRid(rId));
    }

    @RequestMapping("/updRole_Menu")
    @ResponseBody
    public String updRole_Menu(@RequestParam("check[]") List<Integer> check,@RequestParam Integer rId) {
        boolean bo=userService.updRole_Menu(rId, check);
        return JSON.toJSONString(bo);
    }

}
